import { BadRequestException, Injectable } from '@nestjs/common'
import { CreateArticelDto } from './dto/create-articel.dto'
import { UpdateArticelDto } from './dto/update-articel.dto'
import { PrismaService } from 'src/common/prisma/prisma.service'
import { User } from '@prisma/client'
import { ConfigService } from 'src/common/config/config.service'

@Injectable()
export class ArticelService {
   constructor(private readonly prisma: PrismaService, private readonly configServie: ConfigService) {}
   async create(createArticelDto: CreateArticelDto, user: User) {
      const res = await this.prisma.articel.create({
         data: {
            title: createArticelDto.title,
            content: createArticelDto.content,
            previewImg: createArticelDto.previewImg,
            user: { connect: { id: +user.id } },
            category: { connect: { id: +createArticelDto.categoryId } }
         }
      })
      if (!res) throw new BadRequestException('出错了，发布失败')
      return res
   }

   async findAll(categoryId: number, page = 1, userId: number) {
      const where = categoryId == 0 ? 'userId' : 'categoryId'
      const id = +categoryId == 0 ? +userId : +categoryId
      if (categoryId != 1) {
         return await this.prisma.articel.findMany({
            skip: (page - 1) * 5,
            take: +this.configServie.get('page_limit'),
            where: {
               // 如果 where 的值是 userId，则只保留 userId 的条件，否则同时包含 userId 和 status 的条件
               ...(where === 'userId' ? { [where]: id } : { [where]: id, status: 1 })
            },

            orderBy: {
               id: 'desc'
            },

            include: {
               category: true,
               user: true
            }
         })
      } else {
         return await this.prisma.articel.findMany({
            skip: (page - 1) * 5,
            take: +this.configServie.get('page_limit'),
            orderBy: {
               id: Math.random() < 0.5 ? 'asc' : 'desc'
            },
            where: {
               // 如果 where 的值是 userId，则只保留 userId 的条件，否则同时包含 userId 和 status 的条件
               ...(where === 'userId' ? { [where]: id } : { status: 1 })
            },
            include: {
               user: true
            }
         })
      }
   }

   async findOne(id: number) {
      const res = await this.prisma.articel.findFirst({
         where: {
            id: +id
         },
         include: {
            user: true,
            category: true
         }
      })

      res.user.avatar = res.user.avatar ? res.user.avatar : '../../uploads/avt-2.jpeg'

      return res
   }

   async findByTitle(title: string) {
      const res = await this.prisma.articel.findMany({
         where: {
            title: {
               contains: title
            }
         },
         include: {
            user: true,
            category: true
         }
      })

      return res
   }

   async update(id: number, updateArticelDto: UpdateArticelDto) {
      const res = await this.prisma.articel.update({
         where: {
            id: +id
         },
         data: {
            title: updateArticelDto.title,
            content: updateArticelDto.content,
            previewImg: updateArticelDto.previewImg,
            categoryId: +updateArticelDto.categoryId
         }
      })

      return res
   }

   async remove(id: number) {
      const res = await this.prisma.articel.delete({
         where: {
            id
         }
      })

      return res
   }
}
